<?php
//
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$store_id = intval($_RQ['id']);
		$_SESSION['store_id']=$store_id;
	}
	//凭排号下单
	$tableid = intval($_RQ['tableid']);
	$this->session('tableid',$tableid);
	//凭排号下单
	$queueid = intval($_RQ['queueid']);
	$this->session('queueid',$queueid);
	$store_id=$_SESSION['store_id'];
	if($_RQ['sendtype']){
		$sendtype = intval($_RQ['sendtype']);
		$make_type = intval($_RQ['make_type']);
		$this->session('make_type',$make_type);
		if($sendtype==3){
			$make_type=1;
			$sendtype=2;
			$this->session('make_type',$make_type);
		}
		$this->session('sendtype',$sendtype);
	}
	if(isset($_RQ['self_type'])){
		$self_type = intval($_RQ['self_type']);
		$this->session('self_type',$self_type);
	}
	if($_RQ['out_time']){
		$this->session('out_time',$_RQ['out_time']);
	}
	if($_RQ['dayid']){
		$this->session('dayid',$_RQ['dayid']);
	}
	if(!$store_id){
		exi('请先选择门店！','error','jy_weishoppei/search/');
	}
	$store = pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	if($_RQ['scanning'] && $_RQ['table_id']){
		$table_id = intval($_RQ['table_id']);
		$table = pdo_get('jy_weishop_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table_id));
		if($table['status'] <1){
			exi('不好意思，该配点未开放，请选择其他配点！','error','jy_weishoppei/goodslist/');
		}
		if($table['busy']>0 && empty($store['in_add'])){
			exi('配点已被占用，请重新选择配点!','error','jy_weishoppei/goodslist/');
		}
		$this->session('table_id',$table_id);
		pdo_update('jy_weishop_table',array('scanningnum +'=>1,'busy'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$table_id));
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0,'mid'=>0);
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core' && $_SESSION['current_plugin']!='jy_weishop'){//来自其他场景体
		if($return['setting']['is_onegoods']){//开启独立商品
			$params['pluginname']=$_SESSION['current_plugin'];
			$return['params']['pluginname']=$_SESSION['current_plugin'];
		}
	}
	$category = pdo_getall('jy_weishop_goods_group',$params,'','','paixu desc');
	$allnum = pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	$getcarts=model('jy_weishop/cart')->getall(1);
	$carts=$getcarts['list'];
	$goodsprice=$getcarts['goodsprice'];
	foreach($category as &$v){
		$allgoods=pdo_getall('jy_weishop_goods',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate'=>$v['id']),'id');
		$v['total']=0;
		foreach($allgoods as $ag){
			$carttotal=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
			$v['total'] +=$carttotal;
		}
	}
	$allprice = $goodsprice - $youhui;
	$return['store']=$store;
	$return['sendtype']=$this->session('sendtype');
	$return['category']=$category;
	$return['nowckey']=0;
	$return['params']['pcate']=$category[0]['id'];
	$return['allnum']=$allnum;
	$return['totalprice']=$allprice;
	$return['carts']=$carts;
	set_html('title',$store['title'].'-在线'.$return['pluginset']['buytitle']);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$store_id=$_SESSION['store_id'];
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if($_RQ['pcate']){
		$params['pcate']=intval($_RQ['pcate']);
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	if($_RQ['ccate']){
		$params['ccate']=intval($_RQ['ccate']);
		$return['params']['ccate']=intval($_RQ['ccate']);
	}
	if($return['setting']['is_onegoods']){//开启独立商品
		$params['pluginname']='jy_weishoppei';
		$return['params']['pluginname']='jy_weishoppei';
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
	$orderby='salenum desc , id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}else{
		foreach($list as &$itemv){
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				//已选规格
				$selectspecs = array();
				$selespename = '';
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							$selectspecs[$k]=$sp['items'][0]['id'];
							$selespename .=$sp['items'][0]['title'].'，';
						}
				}
				//默认已选参数：
				$spsids = '';
				foreach($selectspecs as $k=>&$s){
						if($k==0){
							$spsids .= $s;
						}else{
							$spsids .= '_'.$s;
						}
				}
				if($store_id){
					$getinfo=model('jy_weishop/store')->getinfo($itemv['id'],$store_id);
					$itemv['total']=$getinfo['id']?$getinfo['total']:$itemv['total'];
					$itemv['price']=$getinfo['id']?$getinfo['price']:$itemv['price'];
					$itemv['options']=model('jy_weishop/store')->getoption($itemv['id'],$store_id,$spsids);
				}
				if(!$itemv['options']['id']){
					$itemv['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>$spsids));
				}
				//默认商品规格价格：
				if($itemv['options']){
					$itemv['price']=$itemv['options']['price'];
					if($itemv['active']['discount']>10){
						$itemv['options']['oldprice']=$itemv['options']['price'];
						$itemv['price']=$itemv['options']['oldprice']*$itemv['active']['discount']/100;
						$itemv['options']['price']=$itemv['price'];
					}
					$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
					$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
				}
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$itemv['optionid']=$itemv['options']['id'];
		}
	}
	exi($list);
}
?>